package com.weitu.worklicense.controller;

import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.dataAuthority.service.DataAuthorityService;
import com.weitu.plugin.PageView;
import com.weitu.worklicense.entity.WorkLicenseApply;
import com.weitu.worklicense.entity.WorkLicenseApplyCriteria;
import com.weitu.worklicense.service.WorkLicenseApplyService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author LiMeng 2017/6/1
 */
@Controller
@RequestMapping(value = "workLicense/today")
public class WorkLicenseTodayController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "workLicense/today";
    private final WorkLicenseApplyService workLicenseApplyService;
    private final DataAuthorityService dataAuthorityService;
    private final AccessoryUtilService accessoryUtilService;

    @Autowired
    public WorkLicenseTodayController(WorkLicenseApplyService workLicenseApplyService
            , DataAuthorityService dataAuthorityService
            , AccessoryUtilService accessoryUtilService) {
        this.workLicenseApplyService = workLicenseApplyService;
        this.dataAuthorityService = dataAuthorityService;
        this.accessoryUtilService = accessoryUtilService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return MODULE_BATH_PATH + "/list";
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String view(Model model, String id) {
        putModelAttributes(model, id);
        return MODULE_BATH_PATH + "/view";
    }

    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request, String sort) {
        WorkLicenseApplyCriteria criteria = new WorkLicenseApplyCriteria();
        String today = DateFormatUtils.ISO_DATE_FORMAT.format(new Date());
        WorkLicenseApplyCriteria.Criteria cr = criteria.createCriteria()
                .andDelStateEqualTo(0)
                .andPlanStartDateLessThanOrEqualTo(today)
                .andPlanEndDateGreaterThanOrEqualTo(today);
        putSearchCriteria(cr);

        int count = workLicenseApplyService.countByCriteria(criteria);
        DataGridPagination dataGridPagination = DataGridPagination.getInstance(request, count);//获取分页对象
        PageView pageView = dataGridPagination.getPageView();//生成pageView分页对象
        //未传sort参数是采用添加时间排序
        if (StringUtils.isEmpty(sort)) {
            pageView.setSortColumn("addTime");
        } else {
            criteria.setOrderByClause(pageView.getSortColumn() + " " + pageView.getOrderType());
        }
        criteria.setPageView(pageView);

        List<WorkLicenseApply> workLicenseApplies = workLicenseApplyService.selectByCriteria(criteria);
        return dataGridPagination.flushDataGridData(workLicenseApplies);
    }

    /**
     * 添加查询条件
     *
     * @param criteria 条件
     */
    private void putSearchCriteria(WorkLicenseApplyCriteria.Criteria criteria) {
        if (StringUtils.isNotEmpty(getPara("workLevelId"))) {
            criteria.andWorkLevelIdEqualTo(getPara("workLevelId"));
        }
        if (StringUtils.isNotEmpty(getPara("workTypeId"))) {
            criteria.andWorkTypeIdEqualTo(getPara("workTypeId"));
        }
        if (StringUtils.isNotEmpty(getPara("applyUserId"))) {
            criteria.andAddUserIdEqualTo(getPara("applyUserId"));
        }
        if (StringUtils.isNotEmpty(getPara("applyDeptId"))) {
            criteria.andAddDeptIdEqualTo(getPara("applyDeptId"));
        }
        if (StringUtils.isNotEmpty(getPara("planStartDate"))) {
            criteria.andPlanStartDateEqualTo(getPara("planStartDate"));
        }
        if (StringUtils.isNotEmpty(getPara("planEndDate"))) {
            criteria.andPlanEndDateEqualTo(getPara("planEndDate"));
        }
        if (StringUtils.isNotEmpty(getPara("applyDateMin"))) {
            criteria.andApplyDateGreaterThanOrEqualTo(getPara("applyDateMin"));
        }
        if (StringUtils.isNotEmpty(getPara("applyDateMax"))) {
            criteria.andApplyDateLessThanOrEqualTo(getPara("applyDateMax"));
        }
        if (StringUtils.isNotEmpty(getPara("addTimeMin"))) {
            criteria.andAddTimeGreaterThanOrEqualTo(getPara("addTimeMin"));
        }
        if (StringUtils.isNotEmpty(getPara("addTimeMax"))) {
            criteria.andAddTimeLessThanOrEqualTo(getPara("addTimeMax"));
        }
        if (StringUtils.isNotEmpty(getPara("addUserId"))) {
            criteria.andAddUserIdEqualTo(getPara("addUserId"));
        }
        if (StringUtils.isNotEmpty(getPara("addDeptId"))) {
            criteria.andAddDeptIdEqualTo(getPara("addDeptId"));
        }
        List<String> deptIds = dataAuthorityService.selectDataDeptIdByUserIdAndDeptId(getSessionUser().getId()
                , getSessionUser().getDepartment().getId());
        if (!deptIds.isEmpty()) {
            criteria.andAddDeptIdIn(deptIds);
        }
    }

    /**
     * 向页面实体添加属性
     *
     * @param model 页面实体
     * @param id    主键id
     */
    private void putModelAttributes(Model model, String id) {
        WorkLicenseApply workLicenseApply = workLicenseApplyService.selectByPrimaryKey(id);
        model.addAttribute("accessoryList", accessoryUtilService.queryAccessoryByBusinessId(id));
        model.addAttribute("workLicenseApply", workLicenseApply);
    }
}
